package kr.co.pension.usr.pe.re.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.pension.admin.pe.ti.dao.PETIDao;
import kr.co.pension.admin.pe.ti.vo.PETIVo;
import kr.co.pension.usr.pe.re.dao.PEREDao;
import kr.co.pension.usr.pe.re.vo.PEREVo;
import kr.co.pension.utils.JspUtil;
import kr.co.pension.utils.sms.SMSUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PEREService {
	@Autowired
	public PEREDao dao;
	
	@Autowired
	public PETIDao petiDao;
	
	
	public List pere100Q(PEREVo vo) throws Exception {
		return dao.pere100Q(vo);
	}
	
	public List pere101Q(PEREVo vo) throws Exception {
		return dao.pere101Q(vo);
	}
	
	
	public List pere200Q(PEREVo vo) throws Exception {
		return dao.pere200Q(vo);
	}
	
	public List pere200_1Q(PEREVo vo) throws Exception {
		return dao.pere200_1Q(vo);
	}
	
	public List pere200_2Q(PEREVo vo) throws Exception {
		return dao.pere200_2Q(vo);
	}
	
	public List pere200_3Q(PEREVo vo) throws Exception {
		return dao.pere200_3Q(vo);
	}
	

	public List pere200_4Q(PEREVo vo) throws Exception {
		return dao.pere200_4Q(vo);
	}

	
	public List pere201Q(PEREVo vo) throws Exception {
		return dao.pere201Q(vo);
	}
	
	public Map pere202_1Q(PEREVo vo) throws Exception {
		return dao.pere202_1Q(vo);
	}
	
	public List pere202_2Q(PEREVo vo) throws Exception {
		return dao.pere202_2Q(vo);
	}
	
	public Map pere202_3Q(PEREVo vo) throws Exception {
		return dao.pere202_3Q(vo);
	}
	
	public Map pere203Q(PEREVo vo) throws Exception {
		return dao.pere203Q(vo);
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void pere204T(Map vo) throws Exception {
		
		PEREVo data = (PEREVo) vo.get("paramMap");
		
		//예약일
		String smsReservation_dt = (String) vo.get("smsReservation_dt");
		smsReservation_dt = smsReservation_dt.replaceAll("-", "");

		PETIVo petiVo = new PETIVo();
		petiVo.setP_id(data.getP_id());
		Map pensionInfo = petiDao.peti300Q(petiVo);
//		Map pensionInfo = service.pere203Q(vo);
		
		Calendar chkDt = Calendar.getInstance();//오늘       
		
		int tm = (Integer) pensionInfo.get("wait_time");
		chkDt.add(Calendar.HOUR,tm);
		
		SimpleDateFormat frmChkDt = new SimpleDateFormat("yyyyMMddHHmmss");
//		System.out.println("설정날짜 : "+ frmChkDt.format(chkDt.getTime())+"");
		
		data.setRm_wait_time(frmChkDt.format(chkDt.getTime()));

		
		if(!"A".equals(data.getAdd_type())){
			//관리자가 입력한게 아니면 무조건 사용자가 입력한거임
			data.setAdd_type("U");
		}
		
		//예약정보에 넣기
		data.setRm_no(dao.pere204_1T(data));
		List addList = (List) vo.get("list");
		//예약정보 객실에 넣기
		if(null!= addList && addList.size()>0){
			for(int i =0; i < addList.size() ; i++){
				Map dt = (Map) addList.get(i);
				data.setPr_no((Integer) dt.get("pr_no"));
				data.setMan_cnt((Integer) dt.get("man_cnt"));
				data.setChi_cnt((Integer) dt.get("chi_cnt"));
				data.setBase_pay((Integer) dt.get("pay"));
				data.setAdd_pay((Integer) dt.get("pay_man_m"));
				if(i==0){
					//연박할인 정보 넣기
					data.setDc_pay((Integer)dt.get("dc_pay") +(Integer)vo.get("rrr_pay"));
					data.setTotal_pay((Integer) dt.get("pay_man_m")+(Integer) dt.get("pay")-(Integer)dt.get("dc_pay")-(Integer)vo.get("rrr_pay"));
				}else{
					data.setDc_pay((Integer)dt.get("dc_pay"));
					data.setTotal_pay((Integer) dt.get("pay_man_m")+(Integer) dt.get("pay")-(Integer)dt.get("dc_pay"));
				}
				
				data.setReservation_dt((String) dt.get("reservation_dt"));
//				data.setPr_no((Integer) dt.get("pr_no"));
				dao.pere204_2T(data);
				
				
				
				//다르 연박설정 풀기
				data.setRel_reservation_dt(vo.get("smsReservation_dt").toString().replaceAll("-", ""));
				List clearDelay = dao.pere204_4Q(data);
				for(int ii = 0;ii < clearDelay.size();ii++){
					
					Map dReservationDt = (Map) clearDelay.get(ii);
					dReservationDt.put("reservation_dt", dReservationDt.get("reservation_dt") );
					dao.pere204_6T(dReservationDt);
					dao.pere204_5T(dReservationDt);
				}
			}
		}

		
		
		List rtAddSvcList = (List) vo.get("rtAddSvcList");
		//예약정보 부가서비스에 넣기
		if(null!= rtAddSvcList && rtAddSvcList.size()>0){
			for(int i =0; i < rtAddSvcList.size() ; i++){
				Map dt = (Map) rtAddSvcList.get(i);
				data.setAs_no((Integer) dt.get("as_no"));
				data.setUse_cnt((Integer) dt.get("use_cnt"));
				data.setReg_msg((String)dt.get("reg_msg"));
				data.setBase_pay((Integer)dt.get("as_pay"));
				data.setTotal_pay((Integer)dt.get("totalPay"));
				dao.pere204_3T(data);
			}
		}
		
		//sms 보내기
		int totalPay = vo.get("totalPay")==null?1:(Integer)vo.get("totalPay");
		String smsRoom_nm = (String) vo.get("smsRoom_nm");
		
		int smsBak = vo.get("smsBak")==null?1:(Integer)vo.get("smsBak");
		int smsRoomCnt = vo.get("smsRoomCnt")==null?1:(Integer)vo.get("smsRoomCnt");
		
		Map smsMap = pere1100Q(data);
		SMSUtil smsUtil = new SMSUtil();
		
		if("Y".equals(pensionInfo.get("sms_send_yn"))){
			int chk_sms_cnt = Integer.parseInt( pensionInfo.get("sms_cnt").toString());
			
			if(chk_sms_cnt > 0){
				if("R".equals(data.getRm_status())){
					//예약대기일때
					if("Y".equals(smsMap.get("i1")) || "Y".equals(smsMap.get("a1"))){
						//사용자에게 보내기
//					[펜션명] 국민은행 234542-01-233243 100.000원 입금대기중 2009-00-00/객실명
//					[예약대기] 2009-00-00/객실명/1박/아무개
						StringBuffer msg = new StringBuffer();
						String send = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						String recive = data.getR_phone1()+"-"+data.getR_phone2()+"-"+data.getR_phone3();
						msg.append("["+pensionInfo.get("p_nm")+"]");
						msg.append(data.getSend_bank());
						msg.append("/");
						
						java.text.DecimalFormat decFormat = new java.text.DecimalFormat("###,###,###,###");

						msg.append(decFormat.format(totalPay) );
						msg.append("원 입금대기중 ");
						msg.append(smsReservation_dt);
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						
						
						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}
					
					if("Y".equals(smsMap.get("i2"))  || "Y".equals(smsMap.get("a2"))){
						//관리자에게 보내기
						StringBuffer msg = new StringBuffer();
//						[예약대기] 2009-00-00/객실명/1박/아무개
						String send = data.getR_phone1()+"-"+data.getR_phone2()+"-"+data.getR_phone3();
						String recive = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						msg.append("[예약대기] ");
						msg.append(smsReservation_dt);
						
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						msg.append("/");
						msg.append(smsBak);
						msg.append("박");
						msg.append("/");
						msg.append(data.getReservation_nm());
						

						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}

				}else if("C".equals(data.getRm_status())){
					//입금확인
					
					if("Y".equals(smsMap.get("j1"))){
						//사용자에게 보내기

		//[펜션명]입금확인 되었습니다/2009-00-00(이용일)/객실명
		//[입금완료]2009-00-00/객실명/1박/아무개
						StringBuffer msg = new StringBuffer();
						String send = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						String recive = data.getR_phone1()+data.getR_phone2()+data.getR_phone3();
						msg.append("["+pensionInfo.get("p_nm")+"] ");
						msg.append("입금확인 되었습니다/");
						msg.append(smsReservation_dt);
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("(이용일)/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						
						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}
					
					if("Y".equals(smsMap.get("j2"))){
						//관리자에게 보내기
						StringBuffer msg = new StringBuffer();
//						[예약대기] 2009-00-00/객실명/1박/아무개
						String send = data.getR_phone1()+"-"+data.getR_phone2()+"-"+data.getR_phone3();
						String recive = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						msg.append("[예약대기] ");
						msg.append(smsReservation_dt);
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						msg.append("/");
						msg.append(smsBak);
						msg.append("박");
						msg.append("/");
						msg.append(data.getReservation_nm());
						

						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}
					//sms 보내기 끝
				}
			}
		}
		
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void pere400T(PEREVo vo) throws Exception {
		//기존 데이터 삭제
		vo.setPm_dt_s(vo.getPm_dts()[0]);
		vo.setPm_dt_e(vo.getPm_dts()[vo.getPm_dts().length-1]);
		dao.pere400_1T(vo);
		
		
		vo.setSite_nm("local");
		vo.setPm_desc("관리자 설정");
		
		//기간내 휴일 데이터 삭제
		List delList = new ArrayList();
		for(int i =0; i< vo.getPm_dts().length;i++){
			delList.add(vo.getPm_dts()[i]);
		}
		vo.setNext_dates(delList);
		dao.pere400_2T(vo);
		
		//아래 휴무데이터 넣을때 사용
		List roomsList = dao.pere400_2Q(vo);
		vo.setNext_dates(roomsList);
		
		for(int i =0; i< vo.getPm_dts().length;i++){
			if(null != vo.getBusy_types() && !"".equals(vo.getBusy_types()[i])){
				vo.setPm_dt(vo.getPm_dts()[i]);
//				vo.setPm_pay(vo.getPm_pays()[i]);
				vo.setPm_per(vo.getPm_pers()[i]);
//				vo.setPm_o_pay(vo.getPm_o_pays()[i]);
				vo.setBusy_type(vo.getBusy_types()[i]);
				vo.setPm_dc_m(vo.getPm_dc_ms()[i]);
				dao.pere400T(vo);
				
				//휴무일경우 reservation_room에 데이터를 넣는다.
				if("0110".equals(vo.getBusy_types()[i])){
					dao.pere400_3T(vo);
				}
			}
		}
		
	}
	
	public List pere400_1Q(PEREVo vo) throws Exception {
		return dao.pere400_1Q(vo);
	}
	
	public List pere501Q(PEREVo vo) throws Exception {
		return dao.pere501Q(vo);
	}
	
	public List pere501_2Q(PEREVo vo) throws Exception {
		return dao.pere501_2Q(vo);
	}
	
	public List pere501_3Q(PEREVo vo) throws Exception {
		return dao.pere501_3Q(vo);
	}
	
	public List pere501_4Q(PEREVo vo) throws Exception {
		return dao.pere501_4Q(vo);
	}
	
	public List pere501_5Q(PEREVo vo) throws Exception {
		return dao.pere501_5Q(vo);
	}
	
	public List pere501_6Q(PEREVo vo) throws Exception {
		return dao.pere501_6Q(vo);
	}
	
	
	public Map pere502_1Q(PEREVo vo) throws Exception {
		return dao.pere502_1Q(vo);
	}
	
	public Map pere502_2Q(PEREVo vo) throws Exception {
		return dao.pere502_2Q(vo);
	}
	
	public Map pere502_3Q(PEREVo vo) throws Exception {
		return dao.pere502_3Q(vo);
	}
	public Map pere502_4Q(PEREVo vo) throws Exception {
		return dao.pere502_4Q(vo);
	}
	
	
	public int pere502_3T(PEREVo vo) throws Exception {
		return dao.pere502_3T(vo);
	}
	
	public void pere502_6T(Map vo) throws Exception {
		dao.pere502_6T(vo);
	}
	
	public int pere502_7T(Map vo) throws Exception {
		return dao.pere502_7T(vo);
	}
	
	public String pere502_8Q(Map vo) throws Exception {
		return dao.pere502_8Q(vo);
	}
	
	public List pere502_9Q(Map vo) throws Exception {
		return dao.pere502_9Q(vo);
	}
	
	public void pere502_10T(Map vo) throws Exception {
		dao.pere502_10T(vo);
	}
	
	
	
	public void pere503_1T(PEREVo vo) throws Exception {
		dao.pere503_1T(vo);
	}
	
	public int pere503_2T(PEREVo vo) throws Exception {
		return dao.pere503_2T(vo);
	}
	
	public int pere503_3T(PEREVo vo) throws Exception {
		return dao.pere503_3T(vo);
	}
	
	public int pere503_4T(PEREVo vo) throws Exception {
		return dao.pere503_4T(vo);
	}
	
	public List pere502_5Q(PEREVo vo) throws Exception {
		return dao.pere502_5Q(vo);
	}
	
	
	public List pere600_1Q(PEREVo vo) throws Exception {
		return dao.pere600_1Q(vo);
	}
	
	public List pere600_2Q(PEREVo vo) throws Exception {
		return dao.pere600_2Q(vo);
	}
	
	public List pere600_3Q(PEREVo vo) throws Exception {
		return dao.pere600_3Q(vo);
	}
	
	
	public void pere600_1T(PEREVo vo) throws Exception {
		dao.pere600_1T(vo);
	}
	
	public int pere600_2T(PEREVo vo) throws Exception {
		return dao.pere600_2T(vo);
	}
	
	public List pere700Q(PEREVo vo) throws Exception {
		return dao.pere700Q(vo);
	}
	
	public Map pere700_1Q(PEREVo vo) throws Exception {
		return dao.pere700_1Q(vo);
	}
	
	public List pere700_2Q(PEREVo vo) throws Exception {
		return dao.pere700_2Q(vo);
	}
	
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void pere700T(PEREVo vo) throws Exception {
		//객실 입금처리
		if(vo.getRm_nos()!=null && vo.getRm_nos().length>0){
			List list = new ArrayList();
			for(int i =0; i< vo.getRm_nos().length;i++){
//				vo.setRm_no(vo.getRm_nos()[i]);
				list.add(vo.getRm_nos()[i]);
			}
			vo.setNext_dates(list);
			//객실 입금처리
			dao.pere700T(vo);
			//부가서비스 입금처리
			dao.pere700_1T(vo);
			
			//sms 보내기
			List smsList = dao.pere700_2Q(vo);
			
			Map smsMap = dao.pere1100Q(vo);
			SMSUtil smsUtil = new SMSUtil();

			PETIVo petiVo = new PETIVo();
			petiVo.setP_id(vo.getP_id());
			Map pensionInfo = petiDao.peti300Q(petiVo);
			
			if(null !=smsList && 0<smsList.size()){
				for(int i =0;i<smsList.size();i++){
					Map map = (Map) smsList.get(i);
					
					//메세지 만들기
					String data =(String) map.get("r_info");
					
					String into = data.split("@")[1];
					int dayCnt = 0;
					String reservation_dt="";
					String room_nm="";
					
					for(int j=0; j<into.split(";").length;j++){
						String[] vals = into.split(";")[j].split(":");
						//입금대기자 정보(부가서비스 가격@객실명:예약일자:기준사람:예약사람:이용일,이용일,...:이용기간:요금;...);...

						String[] val4 = vals[4].split(",");
						for(int k =0; k<val4.length;k++){
							dayCnt++;
							
							if(k==0 && j==0){
								reservation_dt=val4[k].substring(0,4) +"."+val4[k].substring(4,6)+"."+val4[k].substring(6,8);
								room_nm=vals[0];
//								if(into.split(";").length>1){
//									reservation_dt+=".";
//									room_nm+=".";
//								}
							}
						}
					}
					//메세지 만들기 종료
					
					if("Y".equals(pensionInfo.get("sms_send_yn"))){
						int chk_sms_cnt = Integer.parseInt( pensionInfo.get("sms_cnt").toString());
						
						if(chk_sms_cnt > 0){

//							[펜션명]입금확인 되었습니다/2009-00-00(이용일)/객실명		
							if("Y".equals(smsMap.get("c1"))){
								//사용자에게 보내기
								StringBuffer msg = new StringBuffer();
								String send = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
								String recive = (String)map.get("r_phone1")+"-"+(String)map.get("r_phone2")+"-"+(String)map.get("r_phone3");
								msg.append("[");
								msg.append(pensionInfo.get("p_nm"));
								msg.append("] 입금확인 되었습니다/");
								msg.append(reservation_dt);
								if(dayCnt>1){
									msg.append("외 ");
									msg.append((dayCnt-1));
									msg.append("일");
								}
								msg.append("(이용일)/");
								msg.append(room_nm);
								if(into.split(";").length>1){
									msg.append("외 ");
									msg.append((into.split(";").length-1));
									msg.append("객실");
								}
								
								String returnCd = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
								//sms 히스토리에 넣기
								vo.setP_nm((String) pensionInfo.get("p_nm") );
								vo.setFrom_no(send);
								vo.setTo_no(recive);
								vo.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
								vo.setC_code(returnCd);
								vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
								
								pere710Q(vo);
								if("1".equals(returnCd)){
									pere711Q(vo);
								}
								//sms 히스토리에 넣기 종료
								
							}
//							관리자	[입금완료]2009-00-00/객실명/1박/아무개
							if("Y".equals(smsMap.get("c2"))){
								//관리자에게 보내기
								StringBuffer msg = new StringBuffer();
//								[예약대기] 2009-00-00/객실명/1박/아무개
								String send = (String)map.get("r_phone1")+"-"+(String)map.get("r_phone2")+"-"+(String)map.get("r_phone3");
								String recive = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
								msg.append("[입금완료] ");
								
								msg.append(reservation_dt);
								if(dayCnt>1){
									msg.append("외 ");
									msg.append((dayCnt-1));
									msg.append("일");
								}
								msg.append("/");
								msg.append(room_nm);
								if(into.split(";").length>1){
									msg.append("외 ");
									msg.append((into.split(";").length-1));
									msg.append("객실");
								}
								msg.append("/");
								msg.append(dayCnt);
								msg.append("박/");
								msg.append(map.get("reservation_nm"));
								
								String returnCd = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
								//sms 히스토리에 넣기
								vo.setP_nm((String) pensionInfo.get("p_nm") );
								vo.setFrom_no(send);
								vo.setTo_no(recive);
								vo.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
								vo.setC_code(returnCd);
								vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
								
								pere710Q(vo);
								if("1".equals(returnCd)){
									pere711Q(vo);
								}
							}
						}
					}
					
				}	
			}

			//sms 보내기 끝
		}
	}
	
	
	public List pere701Q(PEREVo vo) throws Exception {
		return dao.pere701Q(vo);
	}
	
	
	public List pere702_1Q(PEREVo vo) throws Exception {
		return dao.pere702_1Q(vo);
	}
	
	public List pere702_2Q(PEREVo vo) throws Exception {
		return dao.pere702_2Q(vo);
	}
	
	public Map pere702_3Q(PEREVo vo) throws Exception {
		return dao.pere702_3Q(vo);
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public int pere702T(PEREVo vo) throws Exception {
		
		//환불정보에 값넣기
		dao.pere702T(vo);
		//객실 취소에 환불금 저장
		if(vo.getRrc_nos()!=null && vo.getRrc_nos().length>0){
			for(int i = 0; i < vo.getRrc_nos().length;i++){
				PEREVo dt = new PEREVo();
				dt.setRrc_no(vo.getRrc_nos()[i]);
				dt.setPenalty_pay(vo.getPenalty_pays()[i]);
				dt.setPenalty_per(vo.getPenalty_pers()[i]);
				dao.pere702_1T(dt);
			}
		}
		
		//부가서비스에 환불금 저장
		if(vo.getRm_nos()!=null && vo.getRm_nos().length>0){
			for(int i = 0; i < vo.getRm_nos().length;i++){
				PEREVo dt = new PEREVo();
				dt.setRm_no(vo.getRm_nos()[i]);
				dt.setAs_no(vo.getAs_nos()[i]);
				dt.setPenalty_pay(vo.getPenalty_pays1()[i]);
				dt.setPenalty_per(vo.getPenalty_pers1()[i]);
				dao.pere702_2T(dt);
			}
		}
		
//		[펜션명]아무개님 50,000원 환불완료되었습니다
		Map reservation_mng = pere702_3Q(vo);
		
		Map smsMap = pere1100Q(vo);
		PETIVo petiVo = new PETIVo();
		petiVo.setP_id(vo.getP_id());
		Map pensionInfo = petiDao.peti300Q(petiVo);
		
		DecimalFormat decFormat = new java.text.DecimalFormat("###,###,###,###");
		SMSUtil smsUtil = new SMSUtil();
		StringBuffer msg = new StringBuffer();
		msg.append("[");
		msg.append(pensionInfo.get("p_nm"));
		msg.append("] ");
		msg.append(reservation_mng.get("reservation_nm"));
		msg.append("님 ");
		msg.append(decFormat.format(vo.getPenalty_pay()) );
		msg.append("원 환불완료되었습니다.");
		if("Y".equals(pensionInfo.get("sms_send_yn"))){
			int chk_sms_cnt = Integer.parseInt( pensionInfo.get("sms_cnt").toString());
			
			if(chk_sms_cnt > 0){
				if("Y".equals(smsMap.get("h1")) || "Y".equals(smsMap.get("o1"))){
//					사용자
					
					String send = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
					String recive = (String)reservation_mng.get("r_phone1") +"-"+(String)reservation_mng.get("r_phone2") +"-"+(String)reservation_mng.get("r_phone3") ;


					String returnCd = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
					//sms 히스토리에 넣기
					vo.setP_nm((String) pensionInfo.get("p_nm") );
					vo.setFrom_no(send);
					vo.setTo_no(recive);
					vo.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
					vo.setC_code(returnCd);
					vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
					
					pere710Q(vo);
					if("1".equals(returnCd)){
						pere711Q(vo);
					}
				}
				
				if("Y".equals(smsMap.get("h1")) || "Y".equals(smsMap.get("o2"))){
//					관리자
					String send = (String)reservation_mng.get("r_phone1") +"-"+(String)reservation_mng.get("r_phone2") +"-"+(String)reservation_mng.get("r_phone3") ;
					String recive = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");

					String returnCd = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
					//sms 히스토리에 넣기
					vo.setP_nm((String) pensionInfo.get("p_nm") );
					vo.setFrom_no(send);
					vo.setTo_no(recive);
					vo.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
					vo.setC_code(returnCd);
					vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
					
					pere710Q(vo);
					if("1".equals(returnCd)){
						pere711Q(vo);
					}
				}
				//sms 보내기 끝
			}
		}
		
		
		
		//환불금 반환처리
		return 1;
	}
	
	public List pere703Q(PEREVo vo) throws Exception {
		return dao.pere703Q(vo);
	}
	
	
	/**
	 * 예약자 삭제
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public int pere703T(PEREVo vo) throws Exception {
		
		if(vo.getRm_nos()!=null && vo.getRm_nos().length>0){
			for(int i =0;i < vo.getRm_nos().length;i++){
				int rm_no = (int) vo.getRm_nos()[i];
				vo.setRm_no(rm_no);
				dao.pere703_1T(vo);
				dao.pere703_2T(vo);
				dao.pere703_3T(vo);
			}
		}
		
		return 1;
	}
	
	
	
	public List pere704Q(PEREVo vo) throws Exception {
		return dao.pere704Q(vo);
	}
	
	/**
	 * 예약완료 자료 삭제
	 * @param vo	
	 * @return
	 * @throws Exception
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public int pere704T(PEREVo vo) throws Exception {
		
		if(vo.getRm_nos()!=null && vo.getRm_nos().length>0){
			for(int i =0;i < vo.getRm_nos().length;i++){
				int rm_no = (int) vo.getRm_nos()[i];
				vo.setRm_no(rm_no);
				dao.pere703_1T(vo);
				dao.pere703_2T(vo);
				dao.pere704_1T(vo);
				
				dao.pere703_3T(vo);
			}
		}
		
		return 1;
	}
	
	public List pere705Q(PEREVo vo) throws Exception {
		return dao.pere705Q(vo);
	}
	
	public List pere705_1Q(PEREVo vo) throws Exception {
		return dao.pere705_1Q(vo);
	}	
	public Map pere705_2Q(PEREVo vo) throws Exception {
		return dao.pere705_2Q(vo);
	}
	
	public int pere705_3T(PEREVo vo) throws Exception {
		return dao.pere705_3T(vo);
	}
	
	public int pere705_4T(PEREVo vo) throws Exception {
		return dao.pere705_4T(vo);
	}
	
	public Map pere705_5Q(PEREVo vo) throws Exception {
		return dao.pere705_5Q(vo);
	}
	
	
	public int pere706_1T(PEREVo vo) throws Exception {
		return dao.pere706_1T(vo);
	}
	
	public int pere706_2T(PEREVo vo) throws Exception {
		return dao.pere706_2T(vo);
	}
	
	
	public List pere706_3_1Q(PEREVo vo) throws Exception {
		return dao.pere706_3_1Q(vo);
	}
	
	public List pere706_3_2Q(PEREVo vo) throws Exception {
		return dao.pere706_3_2Q(vo);
	}
	public Map pere706_3_3Q(PEREVo vo) throws Exception {
		return dao.pere706_3_3Q(vo);
	}
	
	
	public List pere707_1Q(PEREVo vo) throws Exception {
		return dao.pere707_1Q(vo);
	}
	
	public List pere707_2Q(PEREVo vo) throws Exception {
		return dao.pere707_2Q(vo);
	}
	
	public int pere707Q(PEREVo vo) throws Exception {
		return dao.pere707Q(vo);
	}
	
	
	
	public void pere710Q(PEREVo vo) throws Exception {
		dao.pere710Q(vo);
	}
	public int pere710_0Q(PEREVo vo) throws Exception {
		return dao.pere710_0Q(vo);
	}
	
	public int pere711Q(PEREVo vo) throws Exception {
		return dao.pere711Q(vo);
	}
	public int pere712Q(PEREVo vo) throws Exception {
		return dao.pere711Q(vo);
	}
	
	
	
	public List pere800_1Q(PEREVo vo) throws Exception {
		return dao.pere800_1Q(vo);
	}
	public Map pere800_2Q(PEREVo vo) throws Exception {
		return dao.pere800_2Q(vo);
	}
	public List pere800_3Q(PEREVo vo) throws Exception {
		return dao.pere800_3Q(vo);
	}
	public List pere800_4Q(PEREVo vo) throws Exception {
		return dao.pere800_4Q(vo);
	}
	public List pere800_5Q(PEREVo vo) throws Exception {
		return dao.pere800_5Q(vo);
	}

	public Map pere901Q(PEREVo vo) throws Exception {
		return dao.pere901Q(vo);
	}
	
	public Map pere1100Q(PEREVo vo) throws Exception {
		return dao.pere1100Q(vo);
	}
	
	public int pere1101T(PEREVo vo) throws Exception {
		return dao.pere1101T(vo);
	}
//	
//	public void pere1400T(Map vo) throws Exception {
////		return dao.pere1101T(vo);
//	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void pere1400T(Map vo) throws Exception {
		
		PEREVo data = (PEREVo) vo.get("paramMap");
		

		PETIVo petiVo = new PETIVo();
		petiVo.setP_id(data.getP_id());
		Map pensionInfo = petiDao.peti300Q(petiVo);
//		Map pensionInfo = service.pere203Q(vo);
		
		Calendar chkDt = Calendar.getInstance();//오늘       
		
		int tm = (Integer) pensionInfo.get("wait_time");
		chkDt.add(Calendar.HOUR,tm);
		
		SimpleDateFormat frmChkDt = new SimpleDateFormat("yyyyMMddHHmmss");
//		System.out.println("설정날짜 : "+ frmChkDt.format(chkDt.getTime())+"");
		
		data.setRm_wait_time(frmChkDt.format(chkDt.getTime()));
		
		if(!"A".equals(data.getAdd_type())){
			//관리자가 입력한게 아니면 무조건 사용자가 입력한거임
			data.setAdd_type("U");
		}
		Map reservationInfo = (Map) vo.get("reservationInfo");
		data.setRm_no(Integer.valueOf(String.valueOf(reservationInfo.get("rm_no"))) );
		
		//예약정보에 넣기
//		data.setRm_no(dao.pere204_1T(data));
		List addList = (List) vo.get("list");
		//예약정보 객실에 넣기
		if(null!= addList && addList.size()>0){
			for(int i =0; i < addList.size() ; i++){
				Map dt = (Map) addList.get(i);
				data.setPr_no((Integer) dt.get("pr_no"));
				data.setMan_cnt((Integer) dt.get("man_cnt"));
				data.setChi_cnt((Integer) dt.get("chi_cnt"));
				data.setBase_pay((Integer) dt.get("base_pay"));
				data.setAdd_pay((Integer) dt.get("add_pay"));
				//추가 수정
				data.setDc_pay((Integer)dt.get("dc_pay"));
				data.setTotal_pay((Integer) dt.get("total_pay"));
				data.setReservation_dt((String) dt.get("reservation_dt"));
				//reservation_rooms에 추가
				dao.pere204_2T(data);

			}
			//reservation_rooms_cancel 삭제
			dao.pere703_1T(data);
		}
		List rtAddSvcList = (List) vo.get("rtAddSvcList");
		//예약정보 부가서비스에 넣기
		if(null!= rtAddSvcList && rtAddSvcList.size()>0){
//			for(int i =0; i < rtAddSvcList.size() ; i++){
//				Map dt = (Map) rtAddSvcList.get(i);
//				data.setAs_no((Integer) dt.get("as_no"));
//				data.setUse_cnt((Integer) dt.get("use_cnt"));
//				data.setReq_msg((String)dt.get("req_msg"));
//				data.setBase_pay((Integer)dt.get("base_pay"));
//				data.setTotal_pay((Integer)dt.get("total_pay"));
//				dao.pere204_3T(data);
//			}
			dao.pere1400T(data);
		}
		
		//sms 보내기
		int totalPay = 0;
		String smsRoom_nm = "";
		String smsReservation_dt = "";
		int smsBak = 0;
		
		
		Map smsData = dao.pere705_5Q(data);
		String r_info ="";
		if("R".equals(data.getRm_status())){
			r_info =(String) smsData.get("r_info");
		}else{
			r_info =(String) smsData.get("c_info");
		}
		String into = r_info.split("@")[1];
//		int smsBak = 0;
//		String reservation_dt="";
//		String room_nm="";
		
		int smsRoomCnt = into.split(";").length;
		totalPay =Integer.parseInt(r_info.split("@")[0]);
		for(int j=0; j<into.split(";").length;j++){
			String[] vals = into.split(";")[j].split(":");
			//입금대기자 정보(부가서비스 가격@객실명:예약일자:기준사람:예약사람:이용일,이용일,...:이용기간:요금;...);...
			totalPay+=Integer.parseInt(vals[6]);
			String[] val4 = vals[4].split(",");
			for(int k =0; k<val4.length;k++){
				smsBak++;
				
				if(k==0 && j==0){
					smsReservation_dt=val4[k].substring(0,4) +"."+val4[k].substring(4,6)+"."+val4[k].substring(6,8);
					smsRoom_nm=vals[0];
				}
			}
		}

		
		Map smsMap = pere1100Q(data);
		SMSUtil smsUtil = new SMSUtil();
		
		if("Y".equals(pensionInfo.get("sms_send_yn"))){
			int chk_sms_cnt = Integer.parseInt( pensionInfo.get("sms_cnt").toString());
			
			if(chk_sms_cnt > 0){
				if("R".equals(data.getRm_status())){
					//예약대기일때
					if("Y".equals(smsMap.get("i1")) || "Y".equals(smsMap.get("a1"))){
						//사용자에게 보내기
//					[펜션명] 국민은행 234542-01-233243 100.000원 입금대기중 2009-00-00/객실명
//					[예약대기] 2009-00-00/객실명/1박/아무개
						StringBuffer msg = new StringBuffer();
						String send = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						String recive = data.getR_phone1()+"-"+data.getR_phone2()+"-"+data.getR_phone3();
						msg.append("["+pensionInfo.get("p_nm")+"]");
						msg.append(data.getSend_bank());
						msg.append("/");
						
						java.text.DecimalFormat decFormat = new java.text.DecimalFormat("###,###,###,###");

						msg.append(decFormat.format(totalPay) );
						msg.append("원 입금대기중 ");
						msg.append(smsReservation_dt);
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						
						
						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}
					
					if("Y".equals(smsMap.get("i2"))  || "Y".equals(smsMap.get("a2"))){
						//관리자에게 보내기
						StringBuffer msg = new StringBuffer();
//						[예약대기] 2009-00-00/객실명/1박/아무개
						String send = data.getR_phone1()+"-"+data.getR_phone2()+"-"+data.getR_phone3();
						String recive = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						msg.append("[예약대기] ");
						msg.append(smsReservation_dt);
						
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						msg.append("/");
						msg.append(smsBak);
						msg.append("박");
						msg.append("/");
						msg.append(data.getReservation_nm());
						

						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}

				}else if("C".equals(data.getRm_status())){
					//입금확인
					
					if("Y".equals(smsMap.get("j1"))){
						//사용자에게 보내기

		//[펜션명]입금확인 되었습니다/2009-00-00(이용일)/객실명
		//[입금완료]2009-00-00/객실명/1박/아무개
						StringBuffer msg = new StringBuffer();
						String send = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						String recive = data.getR_phone1()+data.getR_phone2()+data.getR_phone3();
						msg.append("["+pensionInfo.get("p_nm")+"] ");
						msg.append("입금확인 되었습니다/");
						msg.append(smsReservation_dt);
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("(이용일)/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						
						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}
					
					if("Y".equals(smsMap.get("j2"))){
						//관리자에게 보내기
						StringBuffer msg = new StringBuffer();
//						[예약대기] 2009-00-00/객실명/1박/아무개
						String send = data.getR_phone1()+"-"+data.getR_phone2()+"-"+data.getR_phone3();
						String recive = pensionInfo.get("p_phone1")+"-"+pensionInfo.get("p_phone2")+"-"+pensionInfo.get("p_phone3");
						msg.append("[예약대기] ");
						msg.append(smsReservation_dt);
						if(smsBak>1){
							msg.append("외 ");
							msg.append((smsBak-1));
							msg.append("일");
						}
						msg.append("/");
						msg.append(smsRoom_nm);
						if(smsRoomCnt>1){
							msg.append("외 ");
							msg.append((smsRoomCnt-1));
							msg.append("객실");
						}
						msg.append("/");
						msg.append(smsBak);
						msg.append("박");
						msg.append("/");
						msg.append(data.getReservation_nm());
						

						String returnCode = smsUtil.send(send, recive, JspUtil.getByteSizeToString(msg.toString(),80));
						//sms 히스토리에 넣기
						data.setP_nm((String) pensionInfo.get("p_nm") );
						data.setFrom_no(send);
						data.setTo_no(recive);
						data.setSms_content(JspUtil.getByteSizeToString(msg.toString(),80));
						data.setC_code(returnCode);
						data.setC_msg(smsUtil.smsReturnMsg(data.getC_code()));
						
						dao.pere710Q(data);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							dao.pere711Q(data);
						}
					}
					//sms 보내기 끝
				}
			}
		}
		
	}
	
	
	
	public List pere1700Q(PEREVo vo) throws Exception {
		return dao.pere1700Q(vo);
	}
	
	
	public void pere1800T(PEREVo vo) throws Exception {
		dao.pere1800T(vo);
	}
	
	public int pere1900T(PEREVo vo) throws Exception {
		return dao.pere1900T(vo);
	}
	
	
	public List pere2000Q(PEREVo vo) throws Exception {
		return dao.pere2000Q(vo);
	}
	
	public List pere2100Q(PEREVo vo) throws Exception {
		return dao.pere2100Q(vo);
	}
	
	
	public List pere2200Q(PEREVo vo) throws Exception {
		return dao.pere2200Q(vo);
	}
	
	public List pere1500Q(PEREVo vo) throws Exception {
		return dao.pere1500Q(vo);
	}
	
	
	public int pere1501Q_cnt(PEREVo vo) throws Exception {
		return dao.pere1501Q_cnt(vo);
	}
	public List pere1501Q(PEREVo vo) throws Exception {
		return dao.pere1501Q(vo);
	}
	
	public int pere1502T(PEREVo vo) throws Exception {
		return dao.pere1502T(vo);
	}
	

	public int pere2300T(PEREVo vo) throws Exception {
		return dao.pere2300T(vo);
	}
	
	public List petc100Q(PEREVo vo) throws Exception {
		return dao.petc100Q(vo);
	}
	
	public List petc0101Q(PEREVo vo) throws Exception {
		return dao.petc0101Q(vo);
	}
	
	
	public List petc0200Q(PEREVo vo) throws Exception {
		return dao.petc0200Q(vo);
	}

	public int petc0200RelayQ(Map vo) throws Exception {
		return dao.petc0200RelayQ(vo);
	}	


	public int petc0200Cnt(PEREVo vo) throws Exception {
		return dao.petc0200Cnt(vo);
	}	
	public List petc0200Rooms(PEREVo vo) throws Exception {
		return dao.petc0200Rooms(vo);
	}
	public List petc1003Q(PEREVo vo) throws Exception {
		return dao.petc1003Q(vo);
	}
	
	public void petc1005T(PEREVo vo) throws Exception {
		dao.petc1005T(vo);
	}
	public Map petc1005_1Q(PEREVo vo) throws Exception {
		return dao.petc1005_1Q(vo);
	}
	
	public int petc1005_2T(PEREVo vo) throws Exception {
		return dao.petc1005_2T(vo);
	}
	
	public Map petc1005_3Q(PEREVo vo) throws Exception {
		return dao.petc1005_3Q(vo);
	}
	
	public int petc1005_4T(PEREVo vo) throws Exception {
		return dao.petc1005_4T(vo);
	}
	
	public List petc1004Q(Map vo) throws Exception {
		return dao.petc1004Q(vo);
	}
	
	public List getUsrInfo(Map vo) throws Exception {
		return dao.getUsrInfo(vo);
	}
	
	
}
